// richView.cpp : implementation of the CRichView class
//

#include "../stdafx.h"
#include "../resource.h"
#include "richDoc.h"
#include "CntrItem.h"
#include "richView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

namespace EditRich{
// CRichView

IMPLEMENT_DYNCREATE(CRichView, CRichEditView)

BEGIN_MESSAGE_MAP(CRichView, CRichEditView)
	ON_WM_DESTROY()
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRichEditView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRichEditView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRichEditView::OnFilePrintPreview)
	ON_COMMAND(ID_FORMAT_BOLD, OnFormatBold)
	ON_UPDATE_COMMAND_UI(ID_FORMAT_BOLD, OnUpdateFormatBold)
	ON_COMMAND(ID_FORMAT_ITALIC, OnFormatItalic)
	ON_UPDATE_COMMAND_UI(ID_FORMAT_ITALIC, OnUpdateFormatItalic)
	ON_COMMAND(ID_FORMAT_UNDERLINE, OnFormatUnderline)
	ON_UPDATE_COMMAND_UI(ID_FORMAT_UNDERLINE, OnUpdateFormatUnderline)
END_MESSAGE_MAP()

// CRichView construction/destruction

CRichView::CRichView()
{
	// TODO: add construction code here

}

CRichView::~CRichView()
{
}

BOOL CRichView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CRichEditView::PreCreateWindow(cs);
}

void CRichView::OnInitialUpdate()
{
	CRichEditView::OnInitialUpdate();

	// Set the printing margins (720 twips = 1/2 inch)
	SetMargins(CRect(720, 720, 720, 720));
}


// CRichView printing

BOOL CRichView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}


void CRichView::OnDestroy()
{
	// Deactivate the item on destruction; this is important
	// when a splitter view is being used
   COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
   if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
   {
      pActiveItem->Deactivate();
      ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
   }
   CRichEditView::OnDestroy();
}


void CRichView::OnFormatBold()
{
	SetSomeAttr(CFM_BOLD, CFE_BOLD);
}

void CRichView::OnUpdateFormatBold(CCmdUI* pCmdUI) 
{ 
	pCmdUI->SetCheck(IsSomeAttr(CFM_BOLD, CFE_BOLD));
}

void CRichView::OnFormatItalic()
{
	SetSomeAttr(CFM_ITALIC, CFE_ITALIC);
}

void CRichView::OnUpdateFormatItalic(CCmdUI* pCmdUI) 
{ 
	pCmdUI->SetCheck(IsSomeAttr(CFM_ITALIC, CFE_ITALIC));
}

void CRichView::OnFormatUnderline()
{
	SetSomeAttr(CFM_UNDERLINE, CFE_UNDERLINE);
}

void CRichView::OnUpdateFormatUnderline(CCmdUI* pCmdUI) 
{ 
	pCmdUI->SetCheck(IsSomeAttr(CFM_UNDERLINE, CFE_UNDERLINE));
}

void CRichView::OnFormatStrikeout()
{
	SetSomeAttr(CFM_STRIKEOUT, CFE_STRIKEOUT);
}

void CRichView::OnUpdateFormatStrikeout(CCmdUI* pCmdUI) 
{ 
	pCmdUI->SetCheck(IsSomeAttr(CFM_STRIKEOUT, CFE_STRIKEOUT));
}



void CRichView::SetSomeAttr(DWORD attr, DWORD effect)
{
	CHARFORMAT cf;
	cf.cbSize = sizeof(CHARFORMAT);
	cf.dwMask = attr;

	DWORD dwSelMask = GetRichEditCtrl().GetSelectionCharFormat(cf);
	// If selection is all the same toggle BOLD style
	// turn it on otherwise over the whole selection
	if( (cf.dwMask & attr) & (dwSelMask & attr ) )
	{ cf.dwEffects ^= effect; 
	}
	else
	{ cf.dwEffects |= effect;
	}
	cf.dwMask = attr;
	GetRichEditCtrl().SetSelectionCharFormat(cf);
}

// Character and Font Attributes
int CRichView::IsSomeAttr(DWORD attr, DWORD effect)
{ 
	CHARFORMAT cf;	
	cf.cbSize = sizeof(CHARFORMAT);
	cf.dwMask = CFM_BOLD;

	DWORD dwSelMask = GetRichEditCtrl().GetSelectionCharFormat(cf);
	bool bConsistent = false;
	if( dwSelMask & attr )
	{ 
		bConsistent = true; 
	}
	if( !bConsistent )
	{ 
		return (2); // Set Button to indeterminate
	}
	if( cf.dwEffects & effect )
	{ 
		return(1);   // Set Button to checked
	}
	return 0;        // Set Button to unchecked
}

void CRichView::SetColor(COLORREF color)
{ 
	CHARFORMAT cf;
	cf.cbSize = sizeof(CHARFORMAT);
	cf.dwMask = CFM_COLOR;

	GetRichEditCtrl().GetSelectionCharFormat(cf);
	cf.crTextColor = color;
	if( cf.dwEffects & CFE_AUTOCOLOR )
	{ 
		cf.dwEffects ^= CFE_AUTOCOLOR;
	}
	GetRichEditCtrl().SetSelectionCharFormat(cf);
}

// CRichView diagnostics

#ifdef _DEBUG
void CRichView::AssertValid() const
{
	CRichEditView::AssertValid();
}

void CRichView::Dump(CDumpContext& dc) const
{
	CRichEditView::Dump(dc);
}

CRichDoc* CRichView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRichDoc)));
	return (CRichDoc*)m_pDocument;
}
#endif //_DEBUG
};

// CRichView message handlers
